package Jump;

import javax.swing.*;
import java.awt.*;

public class Character {
    // 角色属性
    public int x;
    public int y;
    public int width = 40;
    public int height = 50;
    public Image headImage = new ImageIcon("img/head.png").getImage();
    public boolean isJumping = false; //跳跃状态
    public boolean isSliding = false; // 滑铲状态
    public int slideDuration = 0; // 滑铲持续时间
    public static final int MAX_SLIDE_DURATION = 25; // 最大滑铲持续时间
    // 奔跑动画
    public int legAnimationCounter = 0;
    public boolean leftLegForward = true;
    public static final int LEG_ANIMATION_SPEED = 5;//换腿时间
    // 游戏常量
    public final int GROUND_HEIGHT;//地面高度
    public int yVelocity = 0; //y轴位置
    public final int GRAVITY = 1;//重力
    public final int JUMP_STRENGTH = 13;//跳跃初速度

    public Character(int groundHeight) {
        this.GROUND_HEIGHT = groundHeight;
        resetPosition();
    }
    //初始化
    public void resetPosition() {
        this.x = 80;
        this.y = JumpPanel.HEIGHT - GROUND_HEIGHT - height;
        legAnimationCounter = 0;
        leftLegForward = true;
        isSliding = false;
        slideDuration = 0;
    }
    //跳跃
    public void jump() {
        if (!isJumping) {
            isJumping = true;
            yVelocity = JUMP_STRENGTH;
            isSliding = false; // 跳跃时结束滑铲
        }
    }
    //滑铲
    public void slide() {
        if (!isSliding && !isJumping) {
            isSliding = true;
            slideDuration = MAX_SLIDE_DURATION;
        }
    }
    //人物状态更新--人物动态
    public void update() {
        // 更新滑铲状态
        if (isSliding) {
            slideDuration--;
            if (slideDuration <= 0) {
                isSliding = false;
            }
        }
        // 更新腿部动画
        if (!isJumping && !isSliding) {
            legAnimationCounter++;
            if (legAnimationCounter >= LEG_ANIMATION_SPEED) {
                legAnimationCounter = 0;
                leftLegForward = !leftLegForward; // 切换腿部位置
            }
        }
        // 更新跳跃物理
        if (isJumping) {
            y -= yVelocity;
            yVelocity -= GRAVITY*1;

            if (y >= JumpPanel.HEIGHT - GROUND_HEIGHT - height) {
                y = JumpPanel.HEIGHT - GROUND_HEIGHT - height;
                isJumping = false;
                yVelocity = 0;
            }
        }
    }
    //画角色
    public void draw(Graphics g) {
        g.setColor(new Color(70, 130, 180));
        // 滑铲状态
        if (isSliding) {
            // 滑铲姿势
            int slideBodyHeight = height - 25;
            int slideBodyWidth = width + 20;
            // 绘制滑铲身体
            g.fillRect(x, y + 25, slideBodyWidth, slideBodyHeight - 5);
            // 绘制头部
            g.drawImage(headImage, x + 5, y + 10, 40, 25, null);
//            g.setColor(new Color(255, 218, 185));
//            g.fillOval(x + 5, y + 10, 30, 25); // 头部位置调整
//            // 绘制眼睛
//            g.setColor(Color.BLACK);
//            g.fillOval(x + 15, y + 20, 5, 5);
//            g.fillOval(x + 25, y + 20, 5, 5);
            // 绘制滑铲的腿部
            g.setColor(new Color(0, 0, 139));
            g.fillRect(x + slideBodyWidth - 15, y + slideBodyHeight + 15, 20, 10);
        } else {
            // 站立/奔跑状态
            // 绘制身体
            g.fillRect(x + 5, y, width - 10, height - 10);
            // 绘制头部
            g.drawImage(headImage, x + 8, y - 25, 35, 35, null);
//            g.setColor(new Color(255, 218, 185));
//            g.fillOval(x + 5, y - 20, 30, 30);
//            // 绘制眼睛
//            g.setColor(Color.BLACK);
//            g.fillOval(x + 15, y - 10, 5, 5);
//            g.fillOval(x + 25, y - 10, 5, 5);
            // 绘制奔跑的腿部
            g.setColor(new Color(0, 0, 139));
            if (leftLegForward) {
                // 左腿在前
                g.fillRect(x + 5, y + height - 10, 10, 20); // 左腿
                g.fillRect(x + 25, y + height - 10, 10, 15); // 右腿（在后）
            } else {
                // 右腿在前
                g.fillRect(x + 5, y + height - 10, 10, 15); // 左腿（在后）
                g.fillRect(x + 25, y + height - 10, 10, 20); // 右腿
            }
        }
    }
    //碰撞框
    public Rectangle getBounds() {
        if (isSliding) {
            // 滑铲时的碰撞框
            return new Rectangle(x, y + 20, width + 20, height - 25);
        }
        // 站立/奔跑时的碰撞框
        return new Rectangle(x + 5, y-20, width - 10, height + 10);
    }
}